package leetcode;

import common.ListNode;

/**
 * @author zhangjunfei
 * @date 2023/7/21 16:04
 */
public class L_24 {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyNode = new ListNode();
        dummyNode.next = head;
        ListNode temp = dummyNode;
        while (temp.next != null && temp.next.next != null) {
            ListNode node1 = temp.next;
            ListNode node2 = temp.next.next;


            temp.next = node2;
            node1.next = node2.next;
            node2.next = node1;
            temp = node1;

        }
        return dummyNode.next;
    }


    //递归解法
    public ListNode swapPairs2(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode next = head.next;
        head.next = swapPairs2(next.next);
        next.next = head;
        return next;

    }

}
